package com.agent;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: fei.wu
 * @Email: fei.wu@rograndec.com
 * @CreateDate: 2019-04-11
 * @Version: 1.0
 */
public class MyClassLoader {

    private final static ConcurrentHashMap<String, MyURLClassLoader> LOADER_CACHE = new ConcurrentHashMap<>();

//    private MyURLClassLoader urlClassLoader;


    public void loadJar(String jarName) throws MalformedURLException {
        MyURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
        if (urlClassLoader != null) {
            return;
        }
        urlClassLoader = new MyURLClassLoader();
//        String path = systemConfig.getExternalClassPath();
        String path = "Volumes/Any/logs/lib";
        URL jarUrl = new URL("jar:file:/" + path + "/" + jarName + "!/");
        urlClassLoader.addURLFile(jarUrl);
        LOADER_CACHE.put(jarName, urlClassLoader);
    }

    public Class loadClass(String jarName, String name) throws ClassNotFoundException {
        MyURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
        if (urlClassLoader == null) {
            return null;
        }
        return urlClassLoader.loadClass(name);
    }

    public void unloadJarFile(String jarName) throws MalformedURLException {
        MyURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
        if (urlClassLoader == null) {
            return;
        }
//        String path = systemConfig.getExternalClassPath();
        String path = "E:/testspace/externalClassPath";
        String jarStr = "jar:file:/" + path + "/" + jarName + "!/";
        urlClassLoader.unloadJarFile(jarStr);
        urlClassLoader = null;
        LOADER_CACHE.remove(jarName);
    }
}
